package com.cyandpos.pad.utils;

import android.content.Context;
import android.graphics.Color;
import androidx.core.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.changyi.device.APApplication;
import com.cyandpos.pad.R;


/**
 * Created by Administrator on 2020/2/7.
 */

public class ToastUtils {

    private static Context context = APApplication.getInstance();
    // Toast对象
    private static Toast toast;
    private static int messageColor = R.color.white;

    /**
     * 在Application中初始化ToastUtils.init(this)
     * @param context
     */
    public static void init(Context context) {
        ToastUtils.context = context.getApplicationContext();
    }

    /**
     * 发送Toast,默认LENGTH_SHORT
     * @param msg
     */
    public static void show(String msg) {
        showCustomToast(context, msg, Toast.LENGTH_SHORT);
    }

    /**
     * 自定义toast的背景色和文字的颜色
     * @param context
     * @param massage
     * @param duration
     */
    private static void showCustomToast(Context context, String massage, int duration) {
        // 设置显示文字的颜色
        SpannableString spannableString = new SpannableString(massage);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, messageColor));
        spannableString.setSpan(colorSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (toast == null) {
            toast = Toast.makeText(context, spannableString, duration);
        } else {
            toast.setText(spannableString);
            toast.setDuration(duration);
        }
        // 设置显示的背景
        View view = toast.getView();
        view.setBackgroundResource(R.drawable.toast_frame_style);
        // 设置Toast要显示的位置，水平居中并在底部，X轴偏移0个单位，Y轴偏移200个单位，
        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 200);
        toast.show();
    }
    /**
     * 在UI界面隐藏或者销毁前取消Toast显示
     */
    public static void cancel() {
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
    }
    public static void showMsg(String content) {
        showMsg(content, 0);
    }

    /**
     * 默认toast的显示
     * @param content
     * @param duration
     */
    public static void showMsg(String content, int duration) {
        if (TextUtils.isEmpty(content))
            return;
        if (duration == 0)
            duration = Toast.LENGTH_SHORT;
        else if (duration == 1)
            duration = Toast.LENGTH_LONG;
        else
            duration = Toast.LENGTH_SHORT;
        if (toast == null){
            toast = Toast.makeText(context, content, duration);
        }else {
            toast.setText(content);
            toast.setDuration(duration);
        }
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
    // 显示信息
    public static void showMsg(Context context, Object msg) {
        if (context == null)
            return;
        Toast toast = new Toast(context);
        TextView tv = new TextView(context);
        tv.setPadding(20, 10, 20, 10);
        tv.setText(msg + "");
        tv.setTextSize(20);
        // tv.setBackgroundResource(R.drawable.toastbackgroud);
        tv.setTextColor(Color.BLACK);
        toast.setView(tv);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }

}
